package co.yixiang.modules.shop.rest;

import co.yixiang.aop.log.Log;
import co.yixiang.modules.shop.domain.MachineSell;
import co.yixiang.modules.shop.service.MachineSellService;
import co.yixiang.modules.shop.service.dto.MachineSellQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @author lijunze
* @date 2020-05-08
*/
@Api(tags = "二手机械出售管理")
@RestController
@RequestMapping("/api/machineSell")
public class MachineSellController {

    private final MachineSellService machineSellService;

    public MachineSellController(MachineSellService machineSellService) {
        this.machineSellService = machineSellService;
    }

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('machineSell:list')")
    public void download(HttpServletResponse response, MachineSellQueryCriteria criteria) throws IOException {
        machineSellService.download(machineSellService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询二手机械出售")
    @ApiOperation("查询二手机械出售")
    @PreAuthorize("@el.check('machineSell:list')")
    public ResponseEntity<Object> getMachineSells(MachineSellQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(machineSellService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增二手机械出售")
    @ApiOperation("新增二手机械出售")
    @PreAuthorize("@el.check('machineSell:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody MachineSell resources){
        return new ResponseEntity<>(machineSellService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改二手机械出售")
    @ApiOperation("修改二手机械出售")
    @PreAuthorize("@el.check('machineSell:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody MachineSell resources){
        machineSellService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除二手机械出售")
    @ApiOperation("删除二手机械出售")
    @PreAuthorize("@el.check('machineSell:del')")
    @DeleteMapping
    public ResponseEntity<Object> deleteAll(@RequestBody Integer[] ids) {
        machineSellService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}